﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infrastructure;
using PickingBusiness;
using Infrastructure.Util;

namespace Picking_Winform
{
    public partial class ConfigSetupView : FormBase
    {
        private DataTable model = new DataTable();

        public ConfigSetupView()
        {
            InitializeComponent();
            this.Controls.Add(base.txtTitle);
            this.comboBox1.Items.Clear();
            this.comboBox1.Items.AddRange(base.items);
            this.comboBox1.SelectedIndex = 0;
        }

        /// <summary>
        /// Get Form Width
        /// </summary>
        /// <returns></returns>
        protected override int GetFormWidth()
        {
            return 820;
        }

        /// <summary>
        /// Set Form Title
        /// </summary>
        private void SetFormTitle()
        {
            this.Text = txtTitle.Text = "系统配置的设定";
        }

        private void ConfigSetupView_Load(object sender, EventArgs e)
        {
            this.GetConfigSetup();
            this.InitConfigInfo();
        }

        private void ConfigSetupView_Shown(object sender, EventArgs e)
        {
            this.SetFormTitle();
        }

        private string GetLineCode()
        {
            string lineCode = ConfigInfo.LineCodeA;

            if (this.comboBox1.SelectedIndex == 0)
            {
                lineCode = ConfigInfo.LineCodeA;
            }
            else if (this.comboBox1.SelectedIndex == 1)
            {
                lineCode = ConfigInfo.LineCodeB;
            }
            else if (this.comboBox1.SelectedIndex == 2)
            {
                lineCode = ConfigInfo.LineCodeC;
            }

            return lineCode;
        }

        /// <summary>
        /// Get Config Setup
        /// </summary>
        private void GetConfigSetup()
        {
            string lineCode = this.GetLineCode();
            this.model = Configuration.GetConfigSetup(lineCode);
        }

        /// <summary>
        /// Init Config Info
        /// </summary>
        private void InitConfigInfo()
        {
            if (this.model == null)
            {
                return;
            }

            foreach (DataRow row in this.model.Rows)
            {
                if (string.Equals("T", row["IsUse"].ToString().ToUpper()))
                {
                    string lineName = row["Line_Name"].ToString().Trim();
                    string lineCode = row["Line_No"].ToString().Trim();
                    string isPrint = row["IsPrint"].ToString().Trim();

                    string workerCount = row["Picking_Worker_No"].ToString().Trim();
                    string maxAddressNo = row["Max_Address_No"].ToString().Trim();
                    string minAddressNo = row["Min_Address_No"].ToString().Trim();

                    this.txtLineCode.Text = lineCode;
                    this.txtLineName.Text = lineName;
                    this.txtMinLightNo.Text = minAddressNo;
                    this.txtMaxLightNo.Text = maxAddressNo;
                    this.txtMaxWorkerNo.Text = workerCount;

                    if (string.Equals("T", isPrint.ToUpper()))
                    {
                        this.chkIsPrint.Checked = true;
                    }
                    else
                    {
                        this.chkIsPrint.Checked = false;
                    }

                    return;
                }
            }
        }

        /// <summary>
        /// Do Validation
        /// </summary>
        /// <returns></returns>
        private bool DoValidation()
        {
            string lineCode = this.txtLineCode.Text.Trim();
            string lineName = this.txtLineName.Text.Trim();
            string minLightNo = this.txtMinLightNo.Text.Trim();
            string maxLightNo = this.txtMaxLightNo.Text.Trim();
            string maxWorkerNo = this.txtMaxWorkerNo.Text.Trim();

            if (string.IsNullOrEmpty(lineCode))
            {
                MessageBox.Show("生产线编号不能为空!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return false;
            }

            if (string.IsNullOrEmpty(lineName))
            {
                MessageBox.Show("生产线名称不能为空!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return false;
            }

            if (string.IsNullOrEmpty(minLightNo))
            {
                MessageBox.Show("最小棚灯号不能为空!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return false;
            }

            if (string.IsNullOrEmpty(maxLightNo))
            {
                MessageBox.Show("最大棚灯号不能为空!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return false;
            }

            if (string.IsNullOrEmpty(maxWorkerNo))
            {
                MessageBox.Show("最大工作人数不能为空!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return false;
            }

            int temp = 0;
            int minLightAddress = 0;
            int maxLightAddress = 0;

            if (!int.TryParse(lineCode, out temp))
            {
                MessageBox.Show("生产线编号必须是个整型数值!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);

                return false;
            }


            if (!int.TryParse(minLightNo, out minLightAddress))
            {
                MessageBox.Show("最小棚灯号必须是个整型数值!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);

                return false;
            }

            if (!int.TryParse(maxLightNo, out maxLightAddress))
            {
                MessageBox.Show("最大棚灯号必须是个整型数值!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);

                return false;
            }

            if (!int.TryParse(maxWorkerNo, out temp))
            {
                MessageBox.Show("最大工作人数必须是个整型数值!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);

                return false;
            }

            if (minLightAddress > maxLightAddress)
            {
                MessageBox.Show("最大棚灯号必须大于或等于最小棚灯号!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);

                return false;
            }

            return true;
        }

        /// <summary>
        /// OK
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.lblInfo.Text = string.Empty;

            string currentTime = int.Parse(DateTime.Now.ToString("HHmm")).ToString("0000");

            if (this.txtPassword.Text.Trim().Length == 4)
            {
                string password = string.Empty;
                password += this.txtPassword.Text.Trim().Substring(3, 1);
                password += this.txtPassword.Text.Trim().Substring(2, 1);
                password += this.txtPassword.Text.Trim().Substring(1, 1);
                password += this.txtPassword.Text.Trim().Substring(0, 1);

                if (string.Equals(currentTime, password))
                {
                    this.pnlPassword.Visible = false;
                    this.pnlConfig.Visible = true;
                    return;
                }
            }

            this.lblInfo.Text = "密码不正确!";
        }

        /// <summary>
        /// Save
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            this.btnSave.Focus();

            if (!this.DoValidation())
            {
                return;
            }

            if (this.model == null)
            {
                return;
            }

            foreach (DataRow row in this.model.Rows)
            {
                if (string.Equals("T", row["IsUse"].ToString().ToUpper()))
                {
                    row["Line_No"] = this.txtLineCode.Text.Trim();
                    row["Line_Name"] = this.txtLineName.Text.Trim();
                    row["Min_Address_No"] = this.txtMinLightNo.Text.Trim();
                    row["Max_Address_No"] = this.txtMaxLightNo.Text.Trim();
                    row["Picking_Worker_No"] = this.txtMaxWorkerNo.Text.Trim();

                    if (this.chkIsPrint.Checked)
                    {
                        row["IsPrint"] = "T";
                    }
                    else
                    {
                        row["IsPrint"] = "F";
                    }
                }
            }

            string lineCode = this.GetLineCode();
            bool result = Configuration.SaveConfigSetup(this.model, lineCode);

            if (result)
            {
                ConfigInfo.InitConfigInfo();
                MessageBox.Show("保存成功!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("保存失败!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Cancel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.btnCancel.Focus();
            this.GetConfigSetup();
            this.InitConfigInfo();
        }

        /// <summary>
        /// Return
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.CloseCurrentWindow();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GetConfigSetup();
            this.InitConfigInfo();
        }
    }
}
